import {defineStore} from "pinia";
export const useUserStore = defineStore("currentUser",{ //名字随便起，全局唯一即可
    state:() => { //全局字段，一般用来读取的
        return{
            curUser:sessionStorage.getItem("user")?JSON.parse(sessionStorage.getItem("user")):null,
            token: sessionStorage.getItem("token")
        };
    },
    actions:{ //全局方法，一般用来修改
        login(user,jwt){
            this.curUser = user;
            this.token = jwt;
            sessionStorage.setItem("user",JSON.stringify(user));
            sessionStorage.setItem("token",jwt);
        },
        logout(){
            this.curUser = null;
            this.token = null;
            sessionStorage.removeItem("user");
            sessionStorage.removeItem("token");
        }
    }
})